package com.shinhye.elephantsafe;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectContactActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selectcontact);
		
		ListView lv = (ListView) findViewById(R.id.lv_contact);
		final List<Map<String, Object>> maplist = getContacts();
		
		lv.setAdapter(new SimpleAdapter(this, maplist,
				R.layout.item_contact_listview, new String[] { "pic", "name",
						"phone" }, new int[] { R.id.iv_contact_pic,
						R.id.tv_contact_name, R.id.tv_contact_phone }));
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String number = (String) maplist.get(position).get("phone");
				Intent intent = new Intent(SelectContactActivity.this, GuardSetup3Activity.class);
				intent.putExtra("number", number);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}

//	private List<Map<String, Object>> getContacts() {
//		List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
//
//		ContentResolver cr = getContentResolver();
//		Uri rawuri = Uri.parse("content://com.android.contacts/raw_contacts");
//		Uri datauri = Uri.parse("content://com.android.contacts/data");
//
//		Cursor rawcursor = cr.query(rawuri, new String[]{"contact_id"}, null, null, null);
//		while(rawcursor.moveToNext()) {
//			String contact_id = rawcursor.getString(0);
//			if(contact_id == null)
//				continue;
//			Map<String, Object> map = new HashMap<String, Object>();
//			Cursor datacursor = cr.query(datauri, new String[]{"mimetype", "data1"}, "raw_contact_id=?", new String[]{contact_id}, null);
//			while(datacursor.moveToNext()) {
//				String mimetype = datacursor.getString(0);
//				String data1 = datacursor.getString(1);
//				map.put("pic", R.drawable.p_1);
//				if("vnd.android.cursor.item/name".equals(mimetype)) {
//					map.put("name", data1);
//				} else if("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
//					map.put("phone", data1);
//				}
//			}
//			maplist.add(map);
//		}
//		return maplist;
//	}
	private List<Map<String, Object>> getContacts() {
		List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();

		ContentResolver cr = getContentResolver();
		Uri rawuri = Uri.parse("content://com.android.contacts/contacts");

		Cursor rawcursor = cr.query(rawuri, new String[]{"_id"}, null, null, null);
        if (rawcursor != null) {
            while(rawcursor.moveToNext()) {
                String contact_id = rawcursor.getString(0);
                if(contact_id == null)
                    continue;
                Map<String, Object> map = new HashMap<String, Object>();
                Uri datauri = Uri.parse("content://com.android.contacts/contacts/" + contact_id + "/data");
                Cursor datacursor = cr.query(datauri, new String[]{"mimetype", "data1"}, null, null, null);
                if (datacursor != null) {
                    while(datacursor.moveToNext()) {
                        String mimetype = datacursor.getString(0);
                        String data1 = datacursor.getString(1);
                        map.put("pic", R.drawable.p_1);
                        if("vnd.android.cursor.item/name".equals(mimetype)) {
                            map.put("name", data1);
                        } else if("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
                            map.put("phone", data1);
                        }
                    }
                    datacursor.close();
                }
                maplist.add(map);
            }
            rawcursor.close();
        }
		return maplist;
	}

}
